// TwnSimDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TwnSim.h"
#include "TwnSimDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define FIX_OP		0
#define FIX_CL		1
#define FIX_UNK		2
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTwnSimDlg dialog

CTwnSimDlg::CTwnSimDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTwnSimDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTwnSimDlg)
	m_bridge = _T("");
	m_clus = _T("");
	m_mm = _T("");
	m_mmRot = _T("");
	m_trolley = _T("");
	m_tvRot = _T("");
	m_tvm = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTwnSimDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTwnSimDlg)
	DDX_Control(pDX, IDC_CHK_MM_CL, m_chkMMClTr);
	DDX_Control(pDX, IDC_CHK_TV_TR, m_chkTVTr);
	DDX_Control(pDX, IDC_CHK_TV, m_chkTV);
	DDX_Control(pDX, IDC_CHK_MM_TR, m_chkMMTr);
	DDX_Control(pDX, IDC_CHK_MM, m_chkMM);
	DDX_Control(pDX, IDC_CMD_FIX, m_cmbFix);
	DDX_Text(pDX, IDC_BRIDGE, m_bridge);
	DDX_Text(pDX, IDC_CLUS, m_clus);
	DDX_Text(pDX, IDC_MM, m_mm);
	DDX_Text(pDX, IDC_MM_ROT, m_mmRot);
	DDX_Text(pDX, IDC_TROLLEY, m_trolley);
	DDX_Text(pDX, IDC_TV_ROT, m_tvRot);
	DDX_Text(pDX, IDC_TVM, m_tvm);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTwnSimDlg, CDialog)
	//{{AFX_MSG_MAP(CTwnSimDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_SET, OnSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTwnSimDlg message handlers

BOOL CTwnSimDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	RETCOND ret = ApmsApiSrvConnect("", &this->m_opStation, NULL);
	if(ret  != R_NORMAL){
		MessageBox("Tenore is not running", "Error", MB_OK | MB_ICONWARNING);
		return FALSE;
	}

	ret = GetTagIdx(TWN_BRIDGE_POS, &this->m_tiBridge);
	if(ret  != R_NORMAL)
		return FALSE;
	
	ret = GetTagIdx(TWN_TROLLEY_POS, &this->m_tiTrolley);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_FAGR_POS, &this->m_tiMM);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_FAGRL_FIXED, &this->m_tiFix);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_FAGRL_UNFIXED, &this->m_tiUnfix);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_CLGR_POS, &this->m_tiClus);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_MM_ROT_POS, &this->m_tiMMRot);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_TVM_POS, &this->m_tiTVM);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_TV_ROT_POS, &this->m_tiTVRot);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_MM_MOUNT, &this->m_tiMMMount);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_TV_MOUNT, &this->m_tiTVMount);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_MM_TRANSPOS, &this->m_tiMMTransp);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_TV_TRANS_POS, &this->m_tiTVTransp);
	if(ret  != R_NORMAL)
		return FALSE;

	ret = GetTagIdx(TWN_MM_CL_TRANSP, &this->m_tiMMClTransp);
	if(ret  != R_NORMAL)
		return FALSE;
	
	this->m_cmbFix.ResetContent();

	this->m_cmbFix.InsertString(FIX_OP, "OPEN");
	this->m_cmbFix.InsertString(FIX_CL, "CLOSED");
	this->m_cmbFix.InsertString(FIX_UNK, "UNKNOWN");
	
	this->ReadVal();

	this->UpdateData(false);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTwnSimDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTwnSimDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTwnSimDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTwnSimDlg::OnExit(){
	ApmsApiSrvDisconnect();
	CDialog::OnCancel();
	
}

RETCOND CTwnSimDlg::ReadVal(void){
	RETCOND			retCnd;
	TAGVALEX		tagVal;
	bool			fixOp;
	bool			fixCl;

	retCnd = TagRead(&this->m_tiBridge, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_bridge.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiTrolley, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_trolley.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiMM, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_mm.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiClus, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_clus.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiMMRot, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_mmRot.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiTVM, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_tvm.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiTVRot, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	this->m_tvRot.Format("%.1f", tagVal.value.val_float);

	retCnd = TagRead(&this->m_tiFix, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		fixCl = true;
	else
		fixCl = false;

	retCnd = TagRead(&this->m_tiUnfix, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		fixOp = true;
	else
		fixOp = false;

	if(fixOp && !fixCl)
		this->m_cmbFix.SetCurSel(FIX_OP);
	else if(!fixOp && fixCl)
		this->m_cmbFix.SetCurSel(FIX_CL);
	else
		this->m_cmbFix.SetCurSel(FIX_UNK);

	retCnd = TagRead(&this->m_tiMMMount, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkMM.SetCheck(1);
	else
		this->m_chkMM.SetCheck(0);

	retCnd = TagRead(&this->m_tiMMMount, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkMM.SetCheck(1);
	else
		this->m_chkMM.SetCheck(0);

	retCnd = TagRead(&this->m_tiMMTransp, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkMMTr.SetCheck(1);
	else
		this->m_chkMMTr.SetCheck(0);

	retCnd = TagRead(&this->m_tiTVMount, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkTV.SetCheck(1);
	else
		this->m_chkTV.SetCheck(0);

	retCnd = TagRead(&this->m_tiTVTransp, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkTVTr.SetCheck(1);
	else
		this->m_chkTVTr.SetCheck(0);

	retCnd = TagRead(&this->m_tiMMClTransp, &tagVal);
	if(retCnd != R_NORMAL)
		return(retCnd);

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
		this->m_chkMMClTr.SetCheck(1);
	else
		this->m_chkMMClTr.SetCheck(0);

	return(R_NORMAL);
}

void CTwnSimDlg::OnSet(){
	RETCOND		retCnd;
	TAGVALEX	tagVal;
	float		val;
	int			retInt;

	this->UpdateData(true);

	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_bridge, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiBridge, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;
	
	ZeroMemory(&tagVal, sizeof(TAGVALEX));
	
	retInt = sscanf_s(this->m_trolley, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiTrolley, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;

	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_mm, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiMM, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;
	
	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_clus, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiClus, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;

	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_mmRot, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiMMRot, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;

	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_tvm, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiTVM, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;

	ZeroMemory(&tagVal, sizeof(TAGVALEX));

	retInt = sscanf_s(this->m_tvRot, "%f", &val);
	if(retInt == 0)
		return;

	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = val;

	retCnd = TagWrite(&this->m_tiTVRot, &tagVal, TAGWRITE_STD);
	if(retCnd != R_NORMAL)
		return;

	if(this->m_cmbFix.GetCurSel() == FIX_OP){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiUnfix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;

		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiFix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else if(this->m_cmbFix.GetCurSel() == FIX_CL){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiFix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;

		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiUnfix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiFix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;

		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiUnfix, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}

	if(this->m_chkMM.GetCheck() == 0){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMMount, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMMount, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}

	if(this->m_chkMMTr.GetCheck() == 0){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}

	if(this->m_chkTV.GetCheck() == 0){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiTVMount, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiTVMount, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}

	if(this->m_chkTVTr.GetCheck() == 0){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiTVTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiTVTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}

	if(this->m_chkMMClTr.GetCheck() == 0){
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMClTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
	else{
		ZeroMemory(&tagVal, sizeof(TAGVALEX));

		tagVal.qual.b.q1imp = 1;
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		StBit((unsigned short *)(&tagVal.value.val_di), 14);

		retCnd = TagWrite(&this->m_tiMMClTransp, &tagVal, TAGWRITE_STD);
		if(retCnd != R_NORMAL)
			return;
	}
}
